﻿using Logs.Formats;
using Logs.Log4net;
using Exceptionless;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using System.Reflection;
using Logs.LogCore.Abstractions;
using Logs.Exceptionless;
using log4net;
using Logs.LogCore.Core;
using Utility.Helpers;

namespace Logs.Extensions
{
    /// <summary>
    /// 日志扩展
    /// </summary>
    public static partial class Extensions
    {
        /// <summary>
        /// 基础Log注册日志操作
        /// </summary>
        /// <param name="services">服务集合</param>
        public static void AddLog(this IServiceCollection services)
        {
            //services.TryAddScoped<ISession, Session>();
            services.TryAddSingleton<ILogFormat, ContentFormat>();
            services.TryAddScoped<ILogContext, LogCore.Core.LogContext>();
            services.TryAddScoped<ILog, Log>();
            ServiceLocator.Instance = services.BuildServiceProvider();
        }

        /// <summary>
        /// 注册NLog日志操作
        /// </summary>
        /// <param name="services">服务集合</param>
        public static void AddNLog(this IServiceCollection services)
        {
            services.TryAddScoped<ILogProviderFactory, NLog.NLogProviderFactory>();
            services.AddLog();
        }

        /// <summary>
        /// 注册Log4net日志操作
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <param name="log4NetConfigFile">log4net配置文件路径</param>
        public static void AddLog4net(this IServiceCollection services, string log4NetConfigFile = "log4net.config")
        {
            var fileNamePath = log4NetConfigFile;
            if (!Path.IsPathRooted(fileNamePath))
            {
                if (!System.IO.File.Exists(fileNamePath))
                {
                    fileNamePath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), fileNamePath);
                }
            }
            fileNamePath = Path.GetFullPath(fileNamePath);

            var repository = LogManager.CreateRepository(Log4Provider.LOG_REPOSITORY_NAME);
            log4net.Config.XmlConfigurator.ConfigureAndWatch(repository, new FileInfo(fileNamePath));
            services.TryAddScoped<ILogProviderFactory, Log4ProviderFactory>();
            services.AddSingleton<ILoggerProvider>(new Log4Provider("", new ContentFormat()));
            services.AddLog();
       
        }

        /// <summary>
        /// Adds the ms log.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        public static void AddMSLog(this IServiceProvider serviceProvider)
        {
            serviceProvider.GetService<ILoggerFactory>().AddProvider(new MSLogging.LoggerProvider());
        }

        /// <summary>
        /// 注册Exceptionless日志操作
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <param name="configAction">配置操作</param>
        public static void AddExceptionless(this IServiceCollection services, Action<ExceptionlessConfiguration> configAction)
        {
            services.TryAddScoped<ILogProviderFactory, LogProviderFactory>();
            services.TryAddSingleton(typeof(ILogFormat), t => NullLogFormat.Instance);
            services.TryAddScoped<ILogContext, Exceptionless.LogContext>();
            services.TryAddScoped<ILog, Log>();
            configAction?.Invoke(ExceptionlessClient.Default.Configuration);
        }
    }
}
